package com.cloudfun.campusshare.service.impl;

import cn.hutool.json.JSONUtil;
import com.cloudfun.campusshare.common.WechatUserHolder;
import com.cloudfun.campusshare.common.constant.IdentityType;
import com.cloudfun.campusshare.common.model.dto.WeChatNotifyBaseDTO;
import com.cloudfun.campusshare.entity.UserAuthEntity;
import com.cloudfun.campusshare.entity.WeChatNotifyEntity;
import com.cloudfun.campusshare.repo.WeChatNotifyEntityRepo;
import com.cloudfun.campusshare.service.UserAuthService;
import com.cloudfun.campusshare.service.WeChatNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Huqin on 2020/4/21
 */
@Slf4j
@Service
public class WeChatNotifyServiceImpl implements WeChatNotifyService {

    @Autowired
    private WeChatNotifyEntityRepo weChatNotifyEntityRepo;

    @Autowired
    private UserAuthService userAuthService;


    @Override
    public <T extends WeChatNotifyBaseDTO> void create(String merchantId, String openId, T notifyDTO) {
        UserAuthEntity userAuthEntity = userAuthService.getByIdentityTypeAndIdentifier(IdentityType.WE_CHAT, openId);
        WechatUserHolder.set(userAuthEntity.getUserId());
        WeChatNotifyEntity weChatNotifyEntity = new WeChatNotifyEntity();
        weChatNotifyEntity.setMerchantId(merchantId);
        weChatNotifyEntity.setCustomerId(userAuthEntity.getUserId());
        weChatNotifyEntity.setContent(JSONUtil.toJsonStr(notifyDTO));
        weChatNotifyEntityRepo.save(weChatNotifyEntity);
    }

}
